package hrms.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import hrms.dao.AdminDao;
import hrms.dao.WantedDao;
import hrms.entity.Wanted;
import hrms.util.PageUtil;
import hrms.util.Result;

@Service("wantedService")
public class WantedServiceImpl implements WantedService{
	
	@Resource(name="adminDao")
	private AdminDao adminDao ;
	
	@Resource(name="wantedDao")
	private WantedDao dao;

	
	public Result<List<Wanted>> findByPage(String pages ,HttpServletResponse res) {
		String message = "Wanted";
		return new PageUtil<Wanted>().findByPage(pages, res, dao, message);
	}
	
	
	public Result<Map<String, Integer>> pageTotal(HttpServletRequest req){
		String message = "Wanted";
		return PageUtil.pageTotal(req, message); 
	}
	
	
	public Result<Object> addWanted(String wjob, String wnum,String wplace,String wtime,String wdate) {
		Result<Object> result = new Result<Object>();
		
		Wanted e = new Wanted();
		e.setwJob(wjob);
		e.setwNum(wnum);
		e.setwPlace(wplace);
		e.setwTime(wtime);
		e.setwDate(wdate);
		e.setwState("0");
		int num = dao.saveWanted(e);
		if(num!=1){
			result.setStatus(1);
			result.setMsg("创建出错！");
		} else {
			result.setStatus(0);
			result.setMsg("创建成功！");
		}
		return result;
	}


	public Result<Object> changeState(String WantedId, HttpServletRequest req) {
		Result<Object> result = new Result<Object>();
		int num = dao.changeState(WantedId);
		
		Cookie[] c = req.getCookies();
		String page = "1";
		for(Cookie cc : c){
			if(cc.getName().equals("WantedPage")){
				page = cc.getValue();
			}
		}
		int pageNum = Integer.parseInt(page);
		if(num != 1){
			result.setStatus(1);
			result.setMsg("修改出错");
		} else {
			result.setStatus(0);
			result.setMsg("修改成功");
			result.setData(pageNum);
		}
		return result;
	}


	public Result<List<Wanted>> search(String name) {
		Result<List<Wanted>> result = new Result<List<Wanted>>();
	    List<Wanted> list = dao.findSearch("%"+name+"%");
		result.setData(list);
		result.setStatus(0);
		return result;
	}


	/*
	public Result<Wanted> findWanted(String WantedId, HttpServletRequest req) {
		Wanted e = dao.findByWantedId(Integer.parseInt(WantedId));
		Result<Wanted> r = new Result<Wanted>();
		if(e != null){
			r.setData(e);
			r.setMsg("");
			r.setStatus(0);
			return r;
		}
		else{
			r.setStatus(1);
			r.setMsg("未找到员工信息！");
			return null;
		}
		
		
	}

	public Result<Object> uppdateWanted(String WantedId, String WantedName, String WantedSex, String WantedAge, String WantedJob,
			String WantedDept,HttpServletRequest req) {
			Result<Object> result = new Result<Object>();
			//获取当前登录的用户
			Admin adminUser = (Admin)req.getSession().getAttribute("admin");
			Integer rollUser = adminUser.getRoll();
			//获取将要被删除的部门号
			
			if(rollUser == 2 ){
				result.setStatus(2);
				result.setMsg("权限不够!");
				return result;
			}
			dao.deleteWanted(Integer.parseInt(WantedId));
			Wanted e = new Wanted();
			e.setWantedId(Integer.parseInt(WantedId));
			e.setAge(Integer.parseInt(WantedAge));
			e.setWantedName(WantedName);
			e.setGender(WantedSex);
			e.setHiredate(new Date(System.currentTimeMillis()));
			String deptId = dao.findDeptIdByName(WantedDept);
			if(deptId == null){
				result.setStatus(3);
				result.setMsg("部门不存在！");
				return result;
			}
			e.setdName(WantedDept);
			e.setDeptId(Integer.parseInt(deptId));
			e.setJob(WantedJob);
			
			int num = dao.updateWanted(e);
			if(num!=1){
				result.setStatus(1);
				result.setMsg("修改出错！");
			} else {
				result.setStatus(0);
				result.setMsg("修改成功！");
			}
		return result;
	}
	*/
}